import sys
import os
import datetime
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *
from pyqtutil import *
class Demo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        layout = QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)
        
        toolBar=QToolBar()
        
        cb = QCheckBox('CalendarPopup')
        cb.stateChanged.connect(self.OnCalendarPopupChanged)
        toolBar.addWidget(cb)

        toolBar.addWidget(QLabel('Section:'))
        section =QComboBox()
        section.addItems(QDateTimeEdit.Section.__members__.keys())
        section.currentTextChanged.connect(self.OnSectionChanged)
        toolBar.addWidget(section)

        toolBar.addWidget(QLabel('DisplayFormat:'))
        self.displayFormat =QLineEdit()
        self.displayFormat.editingFinished.connect(self.SetDisplayFormat)
        toolBar.addWidget(self.displayFormat)

        layout.addWidget(toolBar)
        
        self.dateTimeEdit=QDateTimeEdit()#默认就是本地时间 
        time= datetime.datetime.now()
        
        #dt=QDateTime(time.year,time.month,time.day,time.hour,time.minute,time.second)
        self.dateTimeEdit.setDateTime(PyQtUtil.QDateTime.fromPython(time))
        layout.addWidget(self.dateTimeEdit)

        self.setLayout(layout)
    def OnCalendarPopupChanged(self,state):
        self.dateTimeEdit.setCalendarPopup(state)
        self.parent.OnShowMessage(f"Set CalendarPopup={state}")
    def OnSectionChanged(self,text):
        section = QDateTimeEdit.Section[text]
        self.dateTimeEdit.setSelectedSection(section)        
        self.parent.OnShowMessage(f'Set SelectedSection: {section}')
    def SetDisplayFormat(self):
        format = self.displayFormat.text()
        self.dateTimeEdit.setDisplayFormat(format)
        self.parent.OnShowMessage(f'Set DisplayFormat: {format}')
def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet